package utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.yandex.yandexfotki.R;
import com.yandex.yandexfotki.activities.StartActivity;

import entities.Album;
import entities.ObjectForSend;


public class YandexAPI {
	
	public List<Album> getAlbumsList() {
		return albumsList;
	}
	
	public String getAlbumsUrlList() {
		return albumsListUrl;
	}

	public void setAlbumsList(String albumsList) {
		this.albumsListUrl = albumsList;
	}

	public String getPhotoList() {
		return photoListUrl;
	}

	public void setPhotoList(String photoList) {
		this.photoListUrl = photoList;
	}
	
	public String getToken(){
		return token;
	}

	public YandexAPI(String token) {
		this.token = token;
	}
	
	public void setPhotoAndAlbumsLinks(Document serviceDocument) {
		parseDocument(serviceDocument);
	}
	
	public void addAlbum(Album al) {
		albumsList.add(al);
	}
	
	public void uploadPhoto(File img,String id, String title, String description){
	    UploadPhotoTask upt = new UploadPhotoTask(img,id,title,description);
	    upt.execute();
//		ServerRequest sr = new ServerRequest("POST", URI.create(photoList), params);
//		sr.execute();
//		ByteArrayOutputStream baos = new ByteArrayOutputStream();
//		baos.w
		
	}
	
	public void downloadAlbums() {
		DownloadAlbumsTask dat = new DownloadAlbumsTask();
		dat.execute();
	}
	
	private void parseDocument(Document serviceDocument) {
		Element root = serviceDocument.getDocumentElement();
		parseServiceDocElement(root);
		Log.d("yandex",photoListUrl);
	}
	
	private void parseServiceDocElement(Element root){
		NodeList children = root.getChildNodes();
		for(int i=0; i<children.getLength(); i++) {
			Node child = children.item(i);
			if(child instanceof Element) {
				Element childElement = (Element)child;
				if(childElement.getNodeName().equals("app:collection")) {
					if(childElement.getAttribute("id").equals("album-list")){
						albumsListUrl = childElement.getAttribute("href");
					}
					else if(childElement.getAttribute("id").equals("photo-list")) {
						photoListUrl = childElement.getAttribute("href");
					}
				} else if(childElement.getChildNodes().getLength()!=0){
					parseServiceDocElement(childElement);
				}
			}
		}
	}
	
	private void parseAlbumsFile(Document albumsFile) {
		Element root = albumsFile.getDocumentElement();
		parseAlbumsFileElement(root);
	}
	
	private void parseAlbumsFileElement(Element root) {
		NodeList children = root.getChildNodes();
		for(int i=0; i<children.getLength(); i++) {
			Node child = children.item(i);
			if(child instanceof Element) {
				Element childElement = (Element)child;
				if(childElement.getNodeName().equals("link")){
					if(childElement.getAttribute("rel").equals("next")) {}
				} else if(childElement.getNodeName().equals("entry")) {
					albumsList.add(parseAlbumsEntry(childElement));
				} 
				
			}
		}
	}
	
	public Album parseAlbumsEntry(Element root) {
		
		String id = "";
		String title = "";
		List<String> photos = new ArrayList<String>();
		NodeList children = root.getChildNodes();
		for(int i=0; i<children.getLength(); i++) {
			Node child = children.item(i);
			if(child instanceof Element) {
				Element childElement = (Element)child;
				
				 if(child.getNodeName().equals("title")) {
					title = child.getTextContent();
				} else if(child.getNodeName().equals("f:img")) {
					photos.add(child.getTextContent());
				} else if(child.getNodeName().equals("id")) {
					id = child.getTextContent();
				}
			}
		}
		return new Album(id, title, photos);
	}
	
	
	private class UploadPhotoTask extends ServerRequest {

		@Override
		protected void onPostExecute(Document result) {
			super.onPostExecute(result);
			
		}

		public UploadPhotoTask(File photo,String albumId, String title,String description) {
			this.photo = photo;
			this.albumId = albumId;
			this.title = title;
			this.description = description;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			List<ObjectForSend> params = new ArrayList<ObjectForSend>();
			params.add(new ObjectForSend(ObjectForSend.FILE_CONSTANT_TYPE, photo, "image"));
			if(!albumId.equals("none"))
				params.add(new ObjectForSend(ObjectForSend.STRING_CONSTANT_TYPE, albumId, "album"));
			
			params.add(new ObjectForSend(ObjectForSend.STRING_CONSTANT_TYPE, title, "title"));
			params.add(new ObjectForSend(ObjectForSend.STRING_CONSTANT_TYPE, description, "description"));

			List<NameValuePair> headers = new ArrayList<NameValuePair>();
			headers.add(new BasicNameValuePair("Authorization", "OAuth "+token));
			
			method = HTTPMethod.buildPost(photoListUrl, headers, params, true );
		}
		
		
		
//		 Context ctx;
		File photo;
		String albumId;
		String title;
		String description;
	}
	
	private class DownloadAlbumsTask extends ServerRequest {

		@Override
		protected void onPostExecute(Document result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			parseAlbumsFile(result);
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			List<NameValuePair> headers = new ArrayList<NameValuePair>();
			headers.add(new BasicNameValuePair("Authorization", "OAuth "+token));
			method = HTTPMethod.buildGET(albumsListUrl, headers);
		}
		
		
		
	}
	
	private String token;
	
	private String albumsListUrl;
	private String photoListUrl;
	
	private List<Album> albumsList = new ArrayList<Album>();
}
